<?php
if($_POST['act'] && $_POST['act']=="changeName"){//修改商品名称
    $id = $_POST['id'];
    $name = $_POST['product_name'];
    if($id>0 && !empty($name)){
        $sql = "update ".RECENTORDERPRODUCT." set product_name='{$name}' where id={$id}";
        $db->query($sql);
    }
    die();
}
if($_POST['act'] && $_POST['act']=="changeNum"){//修改商品数量
    $id = $_POST['id'];
    $num = $_POST['num'];
    if($id>0 && !empty($num)){
        $sql = "update ".RECENTORDERPRODUCT." set num='{$num}' where id={$id}";
        $db->query($sql);
    }
    die();
}

if($_GET['act'] && $_GET['act'] == 'showAdd'){//显示添加商品层
    $product_id=$_GET['product_id']?$_GET['product_id']:NULL;
    $pname=$_GET['pname']?$_GET['pname']:NULL;
    $catid=$_GET['catid']?$_GET['catid']:NULL;
    $tags=$_GET['tags']?$_GET['tags']:NULL;
    
    $str = "";
    if($_GET['brand'] && $_GET['brandid']){
        $str.=" and b.brandid = '$_GET[brandid]'";
    }elseif($_GET['brand']){
        $str.=" and b.brand like '".addslashes($_GET[brand])."%'";
    }
    if($_GET['p_stime'])
    {
        $str.=" and IFNULL(a.create_time,0) > '".$_GET[p_stime]."' ";
    }
    if($_GET['p_etime'])
    {
        $str.=" and IFNULL(a.create_time,0) < '".$_GET[p_etime]."' ";
    }
    if(!empty($pname))
        $str.=" and ( a.product_name like '%$pname%' )";
    if(!empty($catid))
        $str.=" and LOCATE($catid,b.catid)=1";
    if(!empty($tags))
        $str.=" and (b.tags like '$tags%' or b.tags like '%,$tags%') ";
    if(!empty($product_id)){
        $str.=" and b.pid in($product_id)";
        $_GET['page_size'] = substr_count($product_id,',')+1;
        $_GET['totalRows'] = $_GET['page_size'];
    }
    if ($_GET['page_size']){
        $page_size=$_GET['page_size'];
    }else{
        $_GET['page_size']=$page_size=20;
    }
    
    $now = date('Y-m-d H:i:s');
    switch ($_GET['order_type'])
    {
        case 1:
            $sql = "select a.*,b.brandid,b.brand,b.tags,b.price,b.show_price,b.market_price,b.catid,b.pid from ".ORPRO." a left join ".SETMEAL." b on a.product_id=b.id where 1 {$str} and b.status>0 GROUP BY a.product_id order by a.create_time desc ";
            break;
        case 2:
            $sql = "select a.*,b.brandid,b.brand,b.tags,b.price,b.show_price,b.market_price,b.catid,b.pid,c.hp_id from ".HUOPINORDERPRO." a left join ".SETMEAL." b on a.sku_id=b.id left join ".HUOPINORDER." c on a.order_id=c.order_id join ".HUOPIN." d on c.hp_id=d.id where 1 {$str} and d.start_time<'{$now}' and d.end_time>'{$now}' GROUP BY a.sku_id order by a.create_time desc ";
            break;
        case 3:
            $sql = "select a.*,b.brandid,b.brand,b.tags,b.price,b.show_price,b.market_price,b.catid,b.pid from ".MICROSTOCKORDERPRO." a left join ".SETMEAL." b on a.sku_id=b.id where 1 {$str} and b.status>0 GROUP BY a.sku_id order by a.create_time desc ";
            break;
        default:
            $sql = "select a.*,b.brandid,b.brand,b.tags,b.price,b.show_price,b.market_price,b.catid,b.pid from ".ORPRO." a left join ".SETMEAL." b on a.product_id=b.id where 1 {$str} and b.status>0 GROUP BY a.product_id order by a.create_time desc ";
            break;
    }
    
    include_once("../includes/page_utf_class.php");
    $page = new Page;
    $page->listRows=$page_size;
    if (!$page->__get('totalRows')){
        $db->query($sql);
        $totalRows = $page->totalRows = $db->num_rows();
    }else{
        $totalRows = $_GET['totalRows'];
    }
    $sql .= "  limit ".$page->firstRow.",".$page->listRows;
    $de['page'] = $page->prompt();
    
    $db->query($sql);
    $de['list']=$db->getRows();
    
    if(!empty($catid) and $de['list'])
    {
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
    }
    elseif(!empty($catid) and !$de['list'])
    {
        $catid=substr($catid,0,-2);
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
    }else{
        $ss=" and catid<9999 ";
    }
    
    $sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
    $db->query($sql);
    $re=$db->getRows();
    if(!$re)
    {
        $catid=substr($catid,0,-2);
        $max=$catid.'99';
        $min=$catid.'00';
        $ss=" and catid<=$max and catid>=$min ";
        $sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
        $db->query($sql);
        $re=$db->getRows();
    }
    $tpl->assign("re",$re);

    include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
    //================================================================
    $pro=new pro();
    $de['all_tags']=$pro->get_tags();

    if ($_GET['tags']){

        $de['tags']=array($_GET['tags']);


    }

    //所属分类，产品标签
    $catids = "";
    $tagids = "";
    $catTmp = array();
    $tagTmp = array();
    foreach ($de['list'] as $dk=>$dv)
    {
        $catids .= $dv['catid'].",";
        $tagids .= $dv['tags'].",";
    }
    $catids = rtrim($catids,",");
    if($catids!=""){
        $sql="SELECT cat,catid FROM ".PCAT." WHERE catid IN (".$catids.")";
        $db->query($sql);
        $catTmp=$db->getRows();
    }
    $catArr = array();
    foreach ($catTmp as $cav){
        $catArr[$cav['catid']] = $cav['cat'];
    }
    $tagids = rtrim($tagids,",");
    if($tagids!=""){
        $sql="SELECT id,tags FROM ".TAG." WHERE id IN (".$tagids.")";
        $db->query($sql);
        $tagTmp=$db->getRows();
    }
    $tagArr = array();
    foreach ($tagTmp as $tav)
    {
        $tagArr[$tav['id']] = $tav['tags'];
    }


    foreach ($de['list'] as $dk=>$dv)
    {
        $de['list'][$dk]['catname'] = $catArr[$dv['catid']];
        $tagsTmp = explode(",", $dv['tags']);
        $tagStr = "";
        foreach ($tagsTmp as $ttv)
        {
            $tagStr .= $tagArr[$ttv].",";
        }
        $de['list'][$dk]['tagnames'] = rtrim($tagStr,",");
    }
    $condTemp = $_GET;
    unset($condTemp['act']);
    $cond = "";
    if(count($condTemp)){
        $cond = "module.php?".http_build_query($condTemp);
    }
    $tpl->assign("postUrl",$cond);
    
    $condTemp1 = $_GET;
    unset($condTemp1['m']);
    unset($condTemp1['s']);
    $cond1 = "";
    if(count($condTemp1)){
        $cond1 = "&".http_build_query($condTemp1);
    }
    $tpl->assign("cond",$cond1);
    
    $tpl->assign('de',$de);
    $tpl->assign("countpro",$totalRows);
    $tpl->display('recent_order_product.htm');
    exit;
}
if($_POST['act']=='add'){//添加商品
    if(!empty($_POST['chk']))
    {
        $_GET['order_type'] = $_GET['order_type']?$_GET['order_type']:1;
        $values = "";
        foreach($_POST['chk'] as $val)
        {
            $tmp = explode(',', $val);
            $proInfo = array();
            $extension = "";
            $sql = "";
            switch ($_GET['order_type']){
                case 1:
                    $sql = "select * from ".ORPRO." where product_id={$tmp[0]} and order_id={$tmp[1]} limit 1";
                    break;
                case 2:
                    $sql = "select * from ".HUOPINORDERPRO." where sku_id={$tmp[0]} and order_id={$tmp[1]} limit 1";
                    break;
                case 3:
                    $sql = "select * from ".MICROSTOCKORDERPRO." where sku_id={$tmp[0]} and order_id={$tmp[1]} limit 1";
                    break;
                default:
                    $sql = "select * from ".ORPRO." where product_id={$tmp[0]} and order_id={$tmp[1]} limit 1";
                    break;
            }
            if(!empty($sql)){
                $db->query($sql);
                $proInfo = $db->fetchRow();
            }
            if(!empty($proInfo)){
                $values .= "(";
                $values .= "'{$_GET['order_type']}','{$proInfo['order_id']}','{$proInfo['member_id']}','{$tmp[0]}','{$tmp[2]}','{$proInfo['pic']}','{$proInfo['product_name']}','{$proInfo['num']}','{$proInfo['create_time']}','{$_SESSION['ADMIN_USER']}'";
                if($_GET['order_type']==2){
                    $extension = json_encode(array('hp_id'=>$tmp[3]));
                }
                $values .= ",'{$extension}'";
                $values .= "),";
            }
        }
        if(!empty($values)){
            $values = trim($values,',');
            $sql = "insert into ".RECENTORDERPRODUCT." (`order_type`,`order_id`,`member_id`,`sku_id`,`pid`,`pic`,`product_name`,`num`,`order_time`,`create_user`,`extension`) values{$values}";
            $db->query($sql);
        }
    }
    echo "<script>window.parent.main.location.reload();</script>";
    echo "<script>window.parent.DialogManager.close('activity');</script>";
    die;
}
if($_POST['act']=='op')
{
    if(!empty($_POST['chk']))
    {
        @$id=implode(",",$_POST['chk']);
        	
        //批量删除
        if($_POST['submit']==$lang['del'] and $id)
        {
            $sql="update ".RECENTORDERPRODUCT." set status=-1 where id in ($id)";
            $db->query($sql);
        }
    }
    //设置顺序
    if($_POST['submit']=='提交' && $_POST['displayorder'])
    {
        foreach($_POST['displayorder'] as $key=>$list)
        {
            $db->query("update ".RECENTORDERPRODUCT." set displayorder='$list' where id='$key'");
        }
    }
    unset($_GET['s']);
    unset($_GET['m']);
    $getstr=implode('&',convert($_GET));
    msg("?m=activity&s=recent_order_product_list.php&$getstr");
}

$product_id=$_GET['product_id']?$_GET['product_id']:NULL;
$str = "";

if(!empty($product_id)){
    $str.=" and pid in($product_id)";
    $_GET['page_size'] = substr_count($product_id,',')+1;
}
if($_GET['order_type']){
    $str .= " and order_type={$_GET['order_type']}";
}
if ($_GET['page_size']){
    $page_size=$_GET['page_size'];
}else{
    $_GET['page_size']=$page_size=20;
}

$sql = "select * from ".RECENTORDERPRODUCT." where status>0 {$str} order by displayorder ";
include_once("../includes/page_utf_class.php");
$page = new Page;
$page->listRows=$page_size;
if (!$page->__get('totalRows')){
    $db->query($sql);
    $totalRows = $page->totalRows = $db->num_rows();
}else{
    $totalRows = $_GET['totalRows'];
}
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();

$db->query($sql);
$de['list']=$db->getRows();


$tpl->assign('de',$de);
$tpl->assign("countpro",$totalRows);
$tpl->display('recent_order_product_list.htm');